package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.util.MybatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    /**
     * 1. 查询所有学生
     * @return
     */
    @Override
    public List<Student> findAll() {
        try(SqlSession session = MybatisUtil.getSqlSession()){
            //1.1 得到Mapper对象
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            //1.2 查询所有学生
            List<Student> students = mapper.findAll();
            //1.3 建立学生与班级的绑定关系
            for (Student student : students) {
                Classes classes = classesMapper.findById(student.getCid());
                student.setCname(classes.getCname());
            }
            return students;
        }

    }

    /**
     * 2. 分页查询
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<Student> findByPage(int page, int pagesize) {
        //2.1 开始分页
        PageHelper.startPage(page,pagesize);
        //2.2 开始查询
        try(SqlSession session = MybatisUtil.getSqlSession()){
            //2.2.1 得到mapper对象
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            //2.2.2 查询出所有学生
            List<Student> students = mapper.findAll();
            //2.2.3 将学生与班级名称进行绑定
            for (Student student : students) {
                Classes classes = classesMapper.findById(student.getCid());
                student.setCname(classes.getCname());
            }
            //2.2.4 将学生集合转换为page
            Page<Student> studentPage = (Page<Student>) students;
            return new PageResult<>(studentPage.getPages(),studentPage.getResult());
        }

    }

    /**
     * 3. 带条件的分页查询
     * @param page
     * @param pagesize
     * @param vo
     * @return
     */
    @Override
    public PageResult<Student> search(int page, int pagesize, StudentVo vo) {
        //3.1 开始分页
        PageHelper.startPage(page,pagesize);
        //3.2 进行查询
        try(SqlSession session = MybatisUtil.getSqlSession()){
            //3.2.1 得到mapper对象
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            //3.2.2 查询出所有学生列表
            List<Student> students = mapper.search(vo);
            //3.2.3 将每一个学生与班级名称进行绑定
            for (Student student : students) {
                String cname = classesMapper.findById(student.getCid()).getCname();
                student.setCname(cname);
            }
            //3.2.4 每个学会僧集合转换为page
            Page<Student> studentPage = (Page<Student>) students;
            //3.3 返回
            return new PageResult<>(studentPage.getPages(),studentPage.getResult());
        }
    }

    //4. 添加学生
    @Override
    public void insert(Student student) {
        try(SqlSession session = MybatisUtil.getSqlSession()){
            //4.1 得到mapper对象
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            //4.2 添加学生
            mapper.insert(student);
        }
    }

    //5. 修改学生
    @Override
    public void update(Student student) {
        try(SqlSession session = MybatisUtil.getSqlSession()){
            //5.1 得到mapper对象
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            //5.2 修改学生
            mapper.update(student);
        }
    }

    //6. 删除学生
    @Override
    public void delete(String sid) {
        try(SqlSession session = MybatisUtil.getSqlSession()){
            //6.1 得到mapper对象
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            //6.2 删除学生
            mapper.delete(sid);
        }
    }
}
